package com.cyw.common.config.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.github.pagehelper.PageInfo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PageInfoJsonSerializer extends JsonSerializer<PageInfo> {
        private ObjectMapper mapper;

        public PageInfoJsonSerializer(ObjectMapper mapper) {
            this.mapper = mapper;
        }

        public void serialize(PageInfo page, JsonGenerator gen, SerializerProvider provider) throws IOException {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("list", page.getList());
            dataMap.put("total", page.getTotal());
            dataMap.put("pageNum", page.getPageNum());
            dataMap.put("pageSize", page.getPageSize());
            dataMap.put("pages", page.getPages());
            this.mapper.writeValue(gen, dataMap);
        }
    }